//
//  File.swift
//  ToastView
//
//  Created by tangjilin on 2025/5/18.
//

import Foundation
#if canImport(UIKit)
import UIKit
#else
// 可选：在macOS上提供替代方案，或抛出编译错误
#endif


import UIKit
import SnapKit

public class ToastView: UIView {
    
    public var backgroundColorCustom: UIColor = .black
    public var textColor: UIColor = .white
    public var cornerRadius: CGFloat = 8.0
    public var font: UIFont = UIFont.systemFont(ofSize: 14)
    public var padding: CGFloat = 16
    
    private let messageLabel = UILabel()
    
    public init(message: String) {
        super.init(frame: .zero)
        setupViews(message: message)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupViews(message: String) {
        self.backgroundColor = backgroundColorCustom
        self.layer.cornerRadius = cornerRadius
        self.clipsToBounds = true
        
        messageLabel.text = message
        messageLabel.textColor = textColor
        messageLabel.font = font
        messageLabel.numberOfLines = 0
        messageLabel.textAlignment = .center
        
        addSubview(messageLabel)
        messageLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(padding)
            make.bottom.equalToSuperview().offset(-padding)
            make.leading.equalToSuperview().offset(padding)
            make.trailing.equalToSuperview().offset(-padding)
        }
    }
    
    public func updatePadding(_ newPadding: CGFloat) {
        self.padding = newPadding
        messageLabel.snp.remakeConstraints { make in
            messageLabel.snp.updateConstraints { make in
                make.edges.equalToSuperview().inset(newPadding)
            }
        }
        setNeedsLayout()
        layoutIfNeeded()
    }
}

/// 顶部、 中心、 底部、 自定义位置
public enum ToastPosition {
    case top
    case center
    case bottom
    case custom(CGPoint)
}

public class Toast {
        
    @MainActor public static func show(
           message: String,
           in view: UIView? = nil,
           position: ToastPosition = .bottom,
           duration: TimeInterval = 2.0,
           animationDuration: TimeInterval = 0.3,
           backgroundColor: UIColor = .black,
           textColor: UIColor = .white,
           cornerRadius: CGFloat = 8,
           font: UIFont = UIFont.systemFont(ofSize: 14),
           padding: CGFloat = 16
       ) {
           guard let superview = view ?? UIApplication.shared.windows.first(where: { $0.isKeyWindow }) else { return }
           
           let toast = ToastView(message: message)
           toast.backgroundColorCustom = backgroundColor
           toast.textColor = textColor
           toast.cornerRadius = cornerRadius
           toast.font = font
           toast.padding = padding
           toast.updatePadding(padding)
           toast.translatesAutoresizingMaskIntoConstraints = false
           
           superview.addSubview(toast)
           
           // 根据位置设置约束
           switch position {
           case .top:
               toast.snp.makeConstraints { make in
                   make.centerX.equalToSuperview()
                   make.top.equalToSuperview().offset(40)
                   make.leading.greaterThanOrEqualToSuperview().offset(padding)
                   make.trailing.lessThanOrEqualToSuperview().offset(-padding)
               }
           case .center:
               toast.snp.makeConstraints { make in
                   make.centerX.equalToSuperview()
                   make.centerY.equalToSuperview()
                   make.leading.greaterThanOrEqualToSuperview().offset(padding)
                   make.trailing.lessThanOrEqualToSuperview().offset(-padding)
               }
           case .bottom:
               toast.snp.makeConstraints { make in
                   make.centerX.equalToSuperview()
                   make.bottom.equalToSuperview().offset(-40)
                   make.leading.greaterThanOrEqualToSuperview().offset(padding)
                   make.trailing.lessThanOrEqualToSuperview().offset(-padding)
               }
           case .custom(let point):
               // 这里的点是相对于 superview 的左上角
               toast.snp.makeConstraints { make in
                   make.centerX.equalToSuperview().offset(point.x)
                   make.centerY.equalToSuperview().offset(point.y)
               }
           }
           
           toast.alpha = 0
           // 动画显示
           UIView.animate(withDuration: animationDuration, animations: {
               toast.alpha = 1
           }) { _ in
               // 两秒后消失
               UIView.animate(withDuration: animationDuration, delay: duration, options: [], animations: {
                   toast.alpha = 0
               }) { _ in
                   toast.removeFromSuperview()
               }
           }
       }
    
}
